package app.netant.book.xiabook;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class XiaBookDetail extends NetAntBase {
	
	private XiaBookEntity entity;
	
	private String exp = "";

	public XiaBookDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(XiaBookEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select(".w990 .weizhi .wznav h5 a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public void analyseTitle(){
		Elements activePart = document.select(".w990 .jianjie .booktitle h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}
	
	public void analyseTags(){
		Elements activePart = document.select(".w990 .jianjie .xinxi ul li");
		for(int i=0;i<activePart.size();i++){
			
			String tags = activePart.get(i).text().trim();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				
				if(tagName.equals("下载格式")){
					if(tagValue.endsWith("txt")){
						this.exp = ".txt";
					}else if(tagValue.endsWith("zip")){
						this.exp = ".zip";
					}else if(tagValue.endsWith("rar")){
						this.exp = ".rar";
					}else if(tagValue.endsWith("epub")){
						this.exp = ".epub";
					}else{
						this.exp = "."+tagValue.substring(tagValue.length()-3);
					}
				}
				
				this.entity.setTag(tagName, tagValue);
				
			}
			
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select(".w990 #zuo .neirong");
		if(activePart.size()>0){
			String memo = activePart.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}
	
	public void analyseHref(){
		Elements activePart = document.select(".w990 #zuo .butt a");
		for(int i=0;i<activePart.size();i++){
			String href = activePart.get(i).attr("href");
			if(href.startsWith("http://www.xiabook.com/down")){
				
				System.out.println("跳转地址开始分析："+href);
				
				XiaBookDetail detail = new XiaBookDetail(href);
				
				String url = detail.getStepHref();
				System.out.println("【Href】:"+url);
				
				this.entity.setDownPath(url);
				
				String name = this.entity.getNovelName();
				this.entity.setNovelFullName(name+this.exp);
				
				break;
			}
		}
	}
	
	public String getStepHref(){
		
		Elements activePart = document.select(".w990 .zw .xzxx p");
		for(int i=0;i<activePart.size();i++){
			Element pPart = activePart.get(i);
			String ex = pPart.text().trim();
			
			if(ex.startsWith("下载格式")){
				int idx = ex.indexOf("：");
				if(idx>0){
					this.exp ="."+ ex.substring(idx+1).trim().toLowerCase();
				}
			}else if(ex.startsWith("点此下载")){
				String href = pPart.select("a").get(0).attr("href");
				return href;
			}
		
		}
		
		throw new RuntimeException("无法获取下载地址");
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		XiaBookDetail detail = new XiaBookDetail("http://www.xiabook.com/mz/19367/");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseHref();
	}
	
}
